<?php

/**
 * 图文回复
 * 
 * Apps/Common/Service/WxNewsReplyService.class.php
 * Zix @ 2015-09-06
 */

namespace Common\Service;

class WxNewsReplyService {

  private static $instance;
  var $type = array(
    1 => '单图文',
    2 => '多图文',
  );
  
  var $status = array(
    0 => '禁用' ,
    1 => '启用' 
  );

  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxNewsReplyService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'type' => 1,
      'pid' => 0,
      'wx_id' => '',
      'sort' => '99',
      'title' => '',
      'desc' => '',
      'icon' => '',
      'url' => '',
      'status' => 1 ,
      'create_time' => date('Y-m-d H:i:s'),
    );
  }

  function get_by_id($id) {
    $WxNewsReply = M('WxNewsReply');
    $data = $WxNewsReply->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'page' => 1,
      'page_size' => 6,
      'status' => '',
      'wx_id' => '',
      'pid' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxNewsReply = M('WxNewsReply');

    $where = array();

    if (!empty($config['status'])) {
      $where['a.status'] = $config['status'];
    }

    if (!empty($config['wx_id'])) {
      $where['a.wx_id'] = $config['wx_id'];
    }

    if ($config['pid'] !== '') {
      $where['a.pid'] = $config['pid'];
    }

    if ($config['count']) {
      return $WxNewsReply->alias('a')->where($where)->count();
    } else {
      $order = 'a.' . $config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxNewsReply
          ->field('a.* , ( select count(*) from wx_news_reply b where b.pid = a.id ) as kids')
          ->alias('a')
          ->where($where)
          ->limit($limit)->order($order)->select();
      //echo $WxNewsReply->_sql();
      return $data ? $data : array();
    }
  }

  function create($data) {
    $WxNewsReply = M('WxNewsReply');
    $ret = $WxNewsReply->add($data);
    if ($ret === FALSE) {
      return ajax_arr('添加失败', FALSE);
    } else {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    }
  }

  function update($id, $data) {
    $WxNewsReply = M('WxNewsReply');

    $ret = $WxNewsReply->where("id = %d", $id)->save($data);
    if ($ret === FALSE) {
      return ajax_arr('编辑失败', FALSE);
    } else {
      return ajax_arr('编辑成功', TRUE);
    }
  }

  function delete($ids) {
    $WxNewsReply = M('WxNewsReply');
    $ret = $WxNewsReply->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
    
  function get_by_keyword( $wx_id , $keyword ) {
    $WxNewsReply = M('WxNewsReply');
    
    $where = array(
      'wx_id' => $wx_id ,
      'keyword' => $keyword ,
      'pid' => 0 ,
      'status' => 1 ,
    );
    $data = $WxNewsReply->where( $where )->limit(1)->select();
    //echo $WxNewsReply->_sql();
    if ( !$data || empty($data ) ) {
      return array() ;
    }
    
    $where2 = array(
      'pid' => $data[0]['id'] ,
      'status' => 1 ,
    );
    $data2 = $WxNewsReply->where($where2)->select();
    
    if ( !$data || empty( $data2 ) ) {
      return $this->_fix_for_api( $data );
    }
    return $this->_fix_for_api( array_merge($data , $data2) );
  }
  
  /**
   * 设置回复图文
   * @param array $newsData
   * 数组结构:
   *  array(
   *  	"0"=>array(
   *  		'Title'=>'msg title',
   *  		'Description'=>'summary text',
   *  		'PicUrl'=>'http://www.domain.com/1.jpg',
   *  		'Url'=>'http://www.domain.com/1.html'
   *  	),
   *  	"1"=>....
   *  )
   */  
  function _fix_for_api( $arr ) {
    $ret = array();
    
    foreach ( $arr as $row ) {
      $ret[] = array(
     		'Title'=>$row['title'],
     		'Description'=>$row['desc'],
     		'PicUrl'=> U($row['icon'] , '' , TRUE , TRUE ),
     		'Url'=>$row['url']
      );
    }
    return $ret;
  }

}
